from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty, ListProperty, NumericProperty,\
    StringProperty, BooleanProperty
from kivy.uix.modalview import ModalView
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.actionbar import ActionBar
from kivy.uix.label import Label
from kivy.adapters.simplelistadapter import SimpleListAdapter

from goblinoid.libs.toast.kivytoast import toast
from goblinoid.profile.profilemodal import ProfileModal

Builder.load_file('goblinoid/comments/commentmodal.kv')


class CommentBar(ActionBar):

    open_previous = BooleanProperty(False)


class CommentModal(ModalView):
    activity = ObjectProperty(None)
    comments = ListProperty()
    comment_layout = ObjectProperty(None)
    index = NumericProperty()
    # sla = ObjectProperty()

    def __init__(self, activity, index, **kwargs):
        super(CommentModal, self).__init__(**kwargs)
        self.activity = activity
        self.comments = activity.obj.comments
        self.index = index

    def on_comments(self, instance, comments):
        pass
        self.build_comment_layout(comments)
        # self.build_sla(comments)

    def build_comment_layout(self, comments):
        # self.build_description_cell()
        for comment in comments:
            cell = CommentModalCell(comment)
            self.comment_layout.add_widget(cell)

    def build_description_cell(self):
        description_cell = ActivityDescriptionCell(self.activity)
        self.comment_layout.add_widget(description_cell)

    # TODO ask Jessica why user_comment.content changes after posting it
    def post_comment(self, comment):
        pump_service = App.get_running_app().pump_service
        user_comment = pump_service.pump.Comment(comment)
        print('post_comment comment: {0}'.format(user_comment.content))
        self.update_feed_view(user_comment)
        cell = CommentModalCell(user_comment)
        self.activity.obj.comment(user_comment)
        self.comment_layout.add_widget(cell, len(self.comment_layout.children))

    def update_feed_view(self, comment_obj):
        App.get_running_app().sm.dispatch('on_comment', self.index, comment_obj)


class CommentLabel(Label):
    pass


class CommentModalCell(BoxLayout):
    comment = ObjectProperty(None)
    comment_text = StringProperty()

    def __init__(self, comment, **kwargs):
        super(CommentModalCell, self).__init__(**kwargs)
        print ('modal cell comment: {0}'.format(comment.content))
        self.comment = comment

    def on_comment(self, instance, comment):
        text = unicode(comment.content)
        # implies author is user. Somewhat hacky way, will come up with a
        # better implementation later
        if comment.author is None:
            author = unicode(App.get_running_app().pump_service.pump.me.username)
        else:
            author = unicode(comment.author.username)

        self.comment_text = unicode('[ref=author][color=86D4B1]{0}[/color][/ref] {1}'.format(author, text))

    def open_profile_modal(self):
        if self.comment.author is None:
            profile = App.get_running_app().pump_service.pump.me
        else:
            profile = self.comment.author
        profile_modal = ProfileModal(profile)
        profile_modal.open()
        

class ActivityDescriptionCell(BoxLayout):
    activity = ObjectProperty(None)
    description = ObjectProperty(None)
    actor = ObjectProperty(None)

    def __init__(self, activity):
        super(ActivityDescriptionCell, self).__init__()
        self.activity = activity
# Note: currently handling Notes/Photo distinction this way
        self.description = activity.content
        self.actor = activity.actor


class BlankSpace(BoxLayout):
    pass
